package com.jxs.android.ges;

import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.Toast;

import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StatusHelper;
import com.jxs.android.util.StringUtils;
import com.jxs.android.weiget.BuAlertDailog;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.PopMsg;
import com.jxs.android.weiget.TopBar;
import com.jxs.android.ges.R;

@SuppressLint("HandlerLeak")
public abstract class AbstractActivity extends Activity {

	protected String TAG = AbstractActivity.class.getSimpleName();

	@SuppressLint("SimpleDateFormat")
	protected static SimpleDateFormat SDF_YYYY_MM_dd_HH_mm = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	protected BuLoadingDailog progressDialog;

	public void initTopBar(Bundle savedInstanceState, int layoutId) {
		if (!this.getClass().getSimpleName().equals(Login.class.getSimpleName())) {
			if (null == GesApplication.getUserInfo() || StringUtils.isEmpety(GesApplication.getUserInfo().getTicket())) {
				errorMsgPop("登录已过期,请重新登录");
				StartHelper.startActivity(Login.class);
				finish();
			}
		}
		if (!StatusHelper.isNetworkAvailable(this)) {
			StatusHelper.checkNetworkAvailable(this);
		}
		super.onCreate(savedInstanceState);
		setContentView(layoutId);
		TopBar topBar = (TopBar) findViewById(R.id.topBar);
		if (null != topBar) {
			topBarLogic(topBar);
		}
	}

	public void topBarLogic(TopBar topBar) {

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!StatusHelper.isNetworkAvailable(this)) {
			StatusHelper.checkNetworkAvailable(this);
			return;
		}
	}

	public void topBarReturn(TopBar topBar) {
		if (null != topBar) {
			topBar.setTopBarReturnListern(new TopBar.ReturnBtnListern() {
				@Override
				public void onClickReturn() {
					finish();
				}
			});
		}
	}

	public void errorMsgPop(int msgId) {
		PopMsg.show(this, msgId);
	}

	public void errorMsgPop(String message) {
		PopMsg.show(this, message);
	}

	public void errorMsgPop(String message, BuAlertDailog.IPhoneAlertDailog alertOkListener) {
		PopMsg.show(this, message, alertOkListener);
	}

	public void errorMsgPop(int msgId, BuAlertDailog.IPhoneAlertDailog alertOkListener) {
		PopMsg.show(this, msgId, alertOkListener);
	}

	public void errorMsgToLogin(String msg) {
		errorMsgPop(msg, new BuAlertDailog.IPhoneAlertDailog() {
			@Override
			public void okLogic(Dialog dialog) {
				dialog.dismiss();
				StartHelper.startActivity(Login.class);
			}

			@Override
			public String getOkText(Context context) {
				return null;
			}
		});
	}

	protected boolean isExit = false;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			isExit = false;
		}

	};

	protected boolean onKeyDownListener(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	private void exit() {
		if (!isExit) {
			isExit = true;
			Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
			mHandler.sendEmptyMessageDelayed(0, 2000);
		} else {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			System.exit(0);
		}
	}
}
